package com.cskaoyan.service.admin;

import com.cskaoyan.bean.MarketAd;
import com.cskaoyan.bean.MarketAdExample;
import com.cskaoyan.mapper.MarketAdMapper;
import com.cskaoyan.model.admin.bo.BaseParam;
import com.cskaoyan.model.admin.vo.BaseDataListVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author hanshuai
 * @version 1.0
 * @description @TODO
 * @date 2021/11/30 22:56
 */
@Service
public class AdminAdServiceImpl implements AdminAdService {
    @Autowired
    MarketAdMapper marketAdMapper;

    @Override
    public BaseDataListVo adList(BaseParam baseParam, String content, String name) {
        PageHelper.startPage(baseParam.getPage(), baseParam.getLimit());
        MarketAdExample example = new MarketAdExample();
        MarketAdExample.Criteria criteria = example.createCriteria();
        if (content != null) {
            criteria.andContentLike("%" + content + "%");
        }
        if (name != null) {
            criteria.andNameLike("%" + name + "%");
        }
        criteria.andDeletedEqualTo(false);
        example.setOrderByClause(baseParam.getSort() + " " + baseParam.getOrder());
        List<MarketAd> marketAds = marketAdMapper.selectByExample(example);
        PageInfo pageInfo = new PageInfo(marketAds);
        BaseDataListVo baseDataListVo = new BaseDataListVo();

        baseDataListVo.setList(marketAds);
        baseDataListVo.setPages(pageInfo.getPages());
        baseDataListVo.setLimit(baseParam.getLimit());
        baseDataListVo.setTotal((int) pageInfo.getTotal());
        baseDataListVo.setPage(baseParam.getPage());

        return baseDataListVo;
    }

    @Override
    public MarketAd adCreate(MarketAd marketAd) {
        Date date = new Date();
        marketAd.setAddTime(date);
        marketAd.setUpdateTime(date);
        marketAdMapper.insertSelective(marketAd);
        return marketAd;
    }

    @Override
    public MarketAd adUpdate(MarketAd marketAd) {
        marketAd.setUpdateTime(new Date());
        MarketAdExample example = new MarketAdExample();
        example.createCriteria().andIdEqualTo(marketAd.getId());
        marketAdMapper.updateByExampleSelective(marketAd, example);
        return marketAd;
    }

    @Override
    public void adDelete(MarketAd marketAd) {
        MarketAdExample example = new MarketAdExample();
        marketAd.setDeleted(true);
        marketAd.setUpdateTime(new Date());
        example.createCriteria().andIdEqualTo(marketAd.getId());
        marketAdMapper.updateByExampleSelective(marketAd,example);
    }
}
